<?php
namespace Frieza\Modules\Decorationtask\Controllers;

use Illuminate\Support\MessageBag;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use App\Admin\Extensions\Tools\UserButton;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Layout\Column;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Doraemon\model\Category;

use Doraemon\service\DecorationTask\Basic as sBasic;
use Doraemon\model\DecorationTask\UserTaskList as mUserTaskList;
use Doraemon\model\MemberDecoration as mMemberDecoration;

class DecorationDiaryController extends Controller
{
    public function list(Content $content,Request $request)
    {
        $params = [
            'page'          => $request->input('page', 1),
            'per_page'      => $request->input('per_page', 20),
            'uid'           => $request->input('uid', ''),
            'nick'          => $request->input('nick', ''),
            'addtime'       => $request->input('addtime', ''),
            '_sort'         => $request->input('_sort', ''),
        ];
        return $content
            ->header('装修日记管理')
            ->breadcrumb(
                ['text' => '装修日记管理列表', 'url' => '/admin/complain/feedback/list']
            )
            ->body($this->listAction($params));
    }

    private function listAction($params)
    {
        $info = sBasic::getDiarytLists($params);
        static $id = 0;
        foreach ($info['data'] as $key => &$value) {
            $value['id'] = $id;
            $value['expand'] = $this->_getPicExpand($value['resultData']);
            $id++;
        }
        $gridModel = new pGridDataModel($info['data'],$info['total'],[
            'perPage' => $params['per_page'],
        ]);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
            });
            // 筛选项
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->column(3/4, function ($filter) {
                    $filter->dropDown('id', [
                        'uid' => '用户uid',
                        'nick' => '用户昵称',
                    ]);
                $filter->between('addtime', '添加时间:')->datetime();
            });
            });
           
            $grid->column('uid')->width('100');
            $grid->column('userNick', '昵称')->expand(function () {
                return $this->expand;
            }, true)->width('400');
            $grid->column('nums', '日记数')->width('200')->sortable();
            $grid->column('addtime', '最新添加时间')->sortable();
        });

        return $listGrid->render();
    }

    public function diaryList($taskListId, $uid, Content $content,Request $request)
    {   
        $params = [
            'page'          => $request->input('page', 1),
            'per_page'      => $request->input('per_page', 20),
        ];
        return $content
            ->header('装修日记管理')
            ->breadcrumb(
                ['text' => '装修日记管理列表', 'url' => '/decorationtask/decorationdiary/list'],
                ['text' => '日记详情列表', 'url' => '']
            )
            ->body($this->diaryListAction($taskListId, $uid, $params));
    }

    private function diaryListAction($taskListId, $uid, $params)
    {
        $info = sBasic::diaryList($taskListId, $uid, $params);
        $gridModel = new pGridDataModel($info['diary_list'],$info['total'],[
            'perPage' => $params['per_page'],
        ]);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableFilter();
           
            $grid->column('task_id')->width('100');
            $grid->column('photo_info', '文字内容')->display(function(){
                $diaryList = $this->photo_info;
                $results = '';
                $results .= date('Y年m月d日', $diaryList['addtime']).'<br />';
                $results .= $diaryList['remark'].'<br />';
              return $results;

            })->width('400');
            $grid->column('content', '视图内容')->display(function($content){
                if (empty($content)) {
                    $res = '';
                } else {
                    if (is_array($content)) {
                        foreach ($content as $key => $value) {
                            $res .='<a href="'.$value.'" target="_blank" style="float:left;padding:5px 5px;"><img src="'.$value.'"  width="100"></a>'; 
                        }
                    } else {
                        $res = '<video width="320" height="240" controls>
                        <source src="'.$content.'" type="video/mp4">
                      </video>';
                    }
                }
                
                return $res;
            })->width('500');
            $grid->column('taskTitle', '所属任务')->display(function(){
                $taskInfo = $this->task_info;
                return $taskInfo['task_title'];
            })->width('200');
            $grid->column('groupInfo', '所属阶段')->display(function(){
                $groupInfo = $this->group_info;
                return $groupInfo['name'];
            })->width('200');
            
        });

        return $listGrid->render();
    }



    private function _getPicExpand($params)
    {
        $row = new Row();
        if ($params) {
            $GirdModel = new pGridDataModel($params, null, ['pageName' => '']);
            $grid = new Grid($GirdModel);
            $grid->disablePagination();
            $grid->disableCreateButton();
            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disableColumnSelector();
            $grid->disableTools();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append('<a href="" ><i class="fa fa-info-circle">查看</i></a>');
            });
            $grid->disableExport();
            $grid->column('taskListId', '任务列表id');
            $grid->column('houseInfo', '房屋信息')->display(function(){
                $houseInfoArr = $this->houseInfo;
                return "{$houseInfoArr['house_name']}<br />面积:{$houseInfoArr['area']}<br />
                        房屋状态：".mMemberDecoration::HOUSE_MAP[$houseInfoArr['status']]."";
            });
            $grid->column('budget', '装修预算')->display(function(){
                $result = $this->budget=='点击设置'?'0.00':$this->budget;

                return $result;
            });
            $grid->column('amount', '目前花费');
            $grid->column('taskAllNum', '任务总数')->help('不包含删除任务数');
            $grid->column('taskDoneNum', '任务详情')->display(function(){
                return "完成任务数:{$this->taskDoneNum}<br />未完成任务数:{$this->taskTodoNum}<br />
                        自定义任务数:{$this->userTaskNum}<br />删除任务数:{$this->delTaskNum}";
            });
            $grid->column('groupStatus', '装修状态');
            $grid->column('taskBills', '记账数');
            $grid->disablePagination();
            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableEdit();
                $actions->disableDelete();
                $actions->append('<a href="/admin/decorationtask/decorationdiary/list/'.$actions->row->taskListId.'/'.$actions->row->uid.'" ><i class="fa fa-info-circle">查看</i></a>');
                $actions->disableView();
            });
            $row->column('12', $grid);
            return $row->render();
        }
        return '';
    }
}
